import { lazy, Suspense } from 'react'
import { Navigate, useRoutes } from 'react-router-dom'

/* import Login from '../pages/Login'
import Main from '../pages/Main'
import NotFind from '../pages/404'

import About from '../pages/Main/About'
import Home from '../pages/Main/Home'

import Music from '../pages/Main/Home/Music'
import News from '../pages/Main/Home/News' */

// Suspense 展示组件
import Loading from '../components/Loading'

const Login = lazy(() => import('../pages/Login'))
const Main = lazy(() => import('../pages/Main'))
const NotFind = lazy(() => import('../pages/404'))

const About = lazy(() => import('../pages/Main/About'))
const Home = lazy(() => import('../pages/Main/Home'))

const Music = lazy(() => import('../pages/Main/Home/Music'))
const News = lazy(() => import('../pages/Main/Home/News'))

// 懒加载路由组件封装
const load = (Compontent) => {
  return (
    <Suspense fallback={<Loading />}>
      <Compontent />
    </Suspense>
  )
}

const routes = [
  {
    path: '/login',
    element: load(Login)
  },
  {
    path: '/main',
    element: load(Main),
    children: [
      {
        path: '/main/about',
        element: load(About)
      },
      {
        path: '/main/home',
        element: load(Home),
        children: [
          {
            path: '/main/home/music/:author',
            element: load(Music)
          },
          {
            path: '/main/home/news/:id',
            element: load(News)
          }
        ]
      },
      {
        path: '/main/',
        element: <Navigate to='/main/about' />
      }
    ]
  },
  {
    path: '/',
    element: <Navigate to='/login' />
  },
  {
    path: '*',
    element: load(NotFind)
  }
]

export default routes

// 返回一个函数式组件 hooks 可以在函数式组件的最外层使用
export const GetRoutes = () => {
  return useRoutes(routes)
}

// 返回一个自定义 hooks ，hooks 可以在其他 hooks 中使用
export const useMakeRoutes = () => {
  return useRoutes(routes)
}